{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57f0514f-d194-4090-9680-84f567d095a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q \"pyiceberg[s3fs]\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e0c0adc-394a-4975-82a6-002fbb27c8ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyiceberg.catalog.rest import RestCatalog\n",
    "import logging\n",
    "\n",
    "# logging.basicConfig(level=logging.DEBUG)\n",
    "\n",
    "import pandas as pd\n",
    "import pyarrow.parquet as pq\n",
    "import pyarrow as pa\n",
    "\n",
    "CATALOG_URL = \"http://lakekeeper:8181/catalog\"\n",
    "DEMO_WAREHOUSE = \"demo\"\n",
    "\n",
    "catalog = RestCatalog(\n",
    "    name=\"my_catalog\",\n",
    "    warehouse=DEMO_WAREHOUSE,\n",
    "    uri=CATALOG_URL,\n",
    "    token=\"dummy\",\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33399f1a-e5ea-4a46-ac60-c4697583e0bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a new namespace if it doesn't already exist\n",
    "test_namespace = (\"pyiceberg_namespace\",)\n",
    "if test_namespace not in catalog.list_namespaces():\n",
    "    catalog.create_namespace(test_namespace)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fa3ce35-4de7-4999-be2a-1e5cfb45f01d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write data\n",
    "test_table = (\"pyiceberg_namespace\", \"my_table\")\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"id\": [1, 2, 3],\n",
    "        \"data\": [\"a\", \"b\", \"c\"],\n",
    "    }\n",
    ")\n",
    "pa_df = pa.Table.from_pandas(df)\n",
    "\n",
    "if test_table in catalog.list_tables(namespace=test_namespace):\n",
    "    catalog.drop_table(test_table)\n",
    "\n",
    "table = catalog.create_table(\n",
    "    test_table,\n",
    "    schema=pa_df.schema,\n",
    ")\n",
    "\n",
    "table.append(pa_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4926d93f-af6f-40fb-8ffe-80e679200df9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read data\n",
    "table = catalog.load_table(test_table)\n",
    "table.scan().to_pandas()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f966e82-e0bb-4fc1-9b45-0bb4988e3d98",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
